package com.greenhouse.business.common.wraning;

/**
 *
 * @版权 : Copyright (c) 2021 儒安物联集团科技有限公司
 * @author: 雷扬
 * @E-mail: leiyang@runaiot.com
 * @版本: 1.0
 * @创建日期: 2021年5月8日 上午10:34:30
 * @ClassName AlarmConfig
 * @类描述-Description:  TODO(这里用一句话描述这个方法的作用)
 * @修改记录:
 * @版本: 1.0
 */

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.jexl3.JexlExpression;

import com.greenhouse.business.domain.BizGroupAlarm;

import lombok.Data;

@Data
public class AlarmConfig {

	private JexlExpression jexlExpression;

	private Long alarmId;

	private String expression;

	private String attributeCode;

	private BizGroupAlarm bizGroupAlarm;

	public AlarmConfig(@NotNull Long alarmId) {
		this.alarmId = alarmId;
	}

	public static List<AlarmConfig> transFromProductWarningDTO(List<BizGroupAlarm> alarms, Evaluator evaluator) {

		List<AlarmConfig> alarmConfigs = new ArrayList<AlarmConfig>();

		if (alarms != null && alarms.size() > 0) {
			for (BizGroupAlarm alarm : alarms) {

				String condition = String.format("attribute.%s!=null && attribute.%s%s%s", alarm.getAttributeCode(),
						alarm.getAttributeCode(), alarm.getType(), alarm.getValue());

				AlarmConfig config = new AlarmConfig(alarm.getAlarmId());
				config.setExpression(condition);
				config.setJexlExpression(evaluator.compile(condition));
				config.setBizGroupAlarm(alarm);
				config.setAttributeCode(alarm.getAttributeCode());
				alarmConfigs.add(config);
			}
		}

		return alarmConfigs;
	}
}